//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ForwardSpreadedTermStructure.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/InterestRate.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Yield/ForwardRateStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::CForwardSpreadedTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ prm1, Cephei::QL::IQuote^ spread) : CForwardRateStructure(CForwardSpreadedTermStructure::typeid)
{
    CYieldTermStructure^ _Cprm1;
    CQuote^ _Cspread;
    try
    {
#ifdef HANDLE
        _phForwardSpreadedTermStructure = NULL;
#endif
        _Cprm1 = safe_cast<CYieldTermStructure^> (prm1);
        _Cprm1->Lock();
        Handle<QuantLib::YieldTermStructure>& _prm1 = static_cast<Handle<QuantLib::YieldTermStructure>&> (_Cprm1->GetHandle ()); 
        _Cspread = safe_cast<CQuote^> (spread);
        _Cspread->Lock();
        Handle<QuantLib::Quote>& _spread = static_cast<Handle<QuantLib::Quote>&> (_Cspread->GetHandle ()); 
        _ppForwardSpreadedTermStructure = new boost::shared_ptr<QuantLib::ForwardSpreadedTermStructure> (new QuantLib::ForwardSpreadedTermStructure ( _prm1,  _spread ));
        SetForwardRateStructure (boost::dynamic_pointer_cast<QuantLib::ForwardRateStructure> (*_ppForwardSpreadedTermStructure));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
        if (_Cspread != nullptr) _Cspread->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::CForwardSpreadedTermStructure (boost::shared_ptr<QuantLib::ForwardSpreadedTermStructure>& childNative, Object^ owner) : CForwardRateStructure(CForwardSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phForwardSpreadedTermStructure = NULL;
#endif
	_ppForwardSpreadedTermStructure = &childNative;
    _ppForwardRateStructure = new boost::shared_ptr<QuantLib::ForwardRateStructure> (boost::dynamic_pointer_cast<QuantLib::ForwardRateStructure> (*_ppForwardSpreadedTermStructure));
}
Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::CForwardSpreadedTermStructure (QuantLib::ForwardSpreadedTermStructure& childNative, Object^ owner) : CForwardRateStructure(CForwardSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phForwardSpreadedTermStructure = NULL;
#endif
	_ppForwardSpreadedTermStructure = new boost::shared_ptr<QuantLib::ForwardSpreadedTermStructure> (&childNative);
    _ppForwardRateStructure = new boost::shared_ptr<QuantLib::ForwardRateStructure> (boost::dynamic_pointer_cast<QuantLib::ForwardRateStructure> (*_ppForwardSpreadedTermStructure));
    _ForwardSpreadedTermStructureOwner = owner;
    _ForwardRateStructureOwner = owner;
}

Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::CForwardSpreadedTermStructure (CForwardSpreadedTermStructure^ copy) : CForwardRateStructure(CForwardSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phForwardSpreadedTermStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppForwardSpreadedTermStructure = new boost::shared_ptr<QuantLib::ForwardSpreadedTermStructure> (copy->GetShared());
        _ppForwardRateStructure = new boost::shared_ptr<QuantLib::ForwardRateStructure> (boost::dynamic_pointer_cast<QuantLib::ForwardRateStructure> (*_ppForwardSpreadedTermStructure));
    }
}
Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::CForwardSpreadedTermStructure (System::Type^ t) : CForwardRateStructure(CForwardSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phForwardSpreadedTermStructure = NULL;
#endif
	if (!t->IsSubclassOf(CForwardSpreadedTermStructure::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::CForwardSpreadedTermStructure (QuantLib::Handle<QuantLib::ForwardSpreadedTermStructure>& childNative, Object^ owner)  : CForwardRateStructure(CForwardSpreadedTermStructure::typeid)
{
	_phForwardSpreadedTermStructure = &childNative;
	_ppForwardSpreadedTermStructure = &static_cast<boost::shared_ptr<QuantLib::ForwardSpreadedTermStructure>>(childNative.currentLink());
    _ppForwardRateStructure = new boost::shared_ptr<QuantLib::ForwardRateStructure> (boost::dynamic_pointer_cast<QuantLib::ForwardRateStructure> (*_ppForwardSpreadedTermStructure));
    _ForwardSpreadedTermStructureOwner = owner;
}
Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::CForwardSpreadedTermStructure (QuantLib::Handle<QuantLib::ForwardSpreadedTermStructure> childNative)  : CForwardRateStructure(CForwardSpreadedTermStructure::typeid)
{
	_phForwardSpreadedTermStructure = &childNative;
	_ppForwardSpreadedTermStructure = &static_cast<boost::shared_ptr<QuantLib::ForwardSpreadedTermStructure>>(childNative.currentLink());
    _ppForwardRateStructure = new boost::shared_ptr<QuantLib::ForwardRateStructure> (boost::dynamic_pointer_cast<QuantLib::ForwardRateStructure> (*_ppForwardSpreadedTermStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::CForwardSpreadedTermStructure (QuantLib::ForwardSpreadedTermStructure childNative)  : CForwardRateStructure(CForwardSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phForwardSpreadedTermStructure = NULL;
#endif
	_ppForwardSpreadedTermStructure = new boost::shared_ptr<QuantLib::ForwardSpreadedTermStructure> (new QuantLib::ForwardSpreadedTermStructure (childNative));
    _ppForwardRateStructure = new boost::shared_ptr<QuantLib::ForwardRateStructure> (boost::dynamic_pointer_cast<QuantLib::ForwardRateStructure> (*_ppForwardSpreadedTermStructure));
}
#endif

Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::~CForwardSpreadedTermStructure ()
{
    if (_ppForwardSpreadedTermStructure != NULL)
    {
	    delete _ppForwardSpreadedTermStructure;
        _ppForwardSpreadedTermStructure = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::!CForwardSpreadedTermStructure ()
{
    if (_ppForwardSpreadedTermStructure != NULL)
    {
	    delete _ppForwardSpreadedTermStructure;
    }
}
QuantLib::ForwardSpreadedTermStructure& Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::GetReference ()
{
    if (_ppForwardSpreadedTermStructure == NULL) throw gcnew NativeNullException ();
	return **_ppForwardSpreadedTermStructure;
}
boost::shared_ptr<QuantLib::ForwardSpreadedTermStructure>& Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::GetShared ()
{
    if (_ppForwardSpreadedTermStructure == NULL) throw gcnew NativeNullException ();
	return *_ppForwardSpreadedTermStructure;
}
QuantLib::ForwardSpreadedTermStructure* Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::GetPointer ()
{
    if (_ppForwardSpreadedTermStructure == NULL) throw gcnew NativeNullException ();
	return &**_ppForwardSpreadedTermStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ForwardSpreadedTermStructure>& Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::GetHandle ()
{
	if (_phForwardSpreadedTermStructure == NULL)
	{
		_phForwardSpreadedTermStructure = new Handle<QuantLib::ForwardSpreadedTermStructure> (*_ppForwardSpreadedTermStructure);
	}
	return *_phForwardSpreadedTermStructure;
}
#endif
bool Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::HasNative () 
{
	return (_ppForwardSpreadedTermStructure != NULL);
}

Cephei::QL::Times::ICalendar^ Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppForwardSpreadedTermStructure)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = gcnew Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppForwardSpreadedTermStructure)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppForwardSpreadedTermStructure)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::MaxTime::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppForwardSpreadedTermStructure)->maxTime ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppForwardSpreadedTermStructure)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure::SettlementDays::get ()
{
    try
    {
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppForwardSpreadedTermStructure)->settlementDays ( );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IForwardSpreadedTermStructure^ Cephei::QL::Termstructures::Yield::CForwardSpreadedTermStructure_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ prm1, Cephei::QL::IQuote^ spread)
{
    return gcnew CForwardSpreadedTermStructure ( prm1,  spread);
}
